//
//  NSFileManager+Category.m
//  RealmeSmartBulb
//
//  Created by lkx on 2020/8/29.
//  Copyright © 2020 isoftstone. All rights reserved.
//

#import "NSFileManager+Category.h"
#import "ZHHomeLog.h"

@implementation NSFileManager (Category)

/// 创建文件夹
+ (NSString *)createFloderWithName:(NSString *)floderName {
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    // sound floder
    NSString *folder = [document stringByAppendingPathComponent:floderName];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    while (![fileManager isExecutableFileAtPath:folder]) {
        BOOL success = [fileManager createDirectoryAtPath:folder withIntermediateDirectories:NO attributes:nil error:nil];
        if (success) {
            break;
        }
        ZHLog(@"创建文件夹: %d", success);
    }
    return folder;
}

/// 创建文件
+ (NSString *)crateFileWithFloder:(NSString *)folder fileName:(NSString *)fileName {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [folder stringByAppendingPathComponent:fileName];
    if (![fileManager isExecutableFileAtPath:folder]) {
        [self createFloderWithName:folder];
    }
    while (![fileManager isExecutableFileAtPath:filePath]) {
        BOOL success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
        if (success) {
            break;
        }
        ZHLog(@"创建文件: %d", success);
    }
    
    return filePath;
}

/// 移动文件
+ (BOOL)moveFileFrom:(NSString *)fromPath toPath:(NSString *)toPath {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    BOOL success = [fileManager moveItemAtPath:fromPath toPath:toPath error:&error];
    if (error) {
        ZHLog(@"移动文件从%@到%@失败: %@", fromPath, toPath, error);
    }
    return success;
}

/// 移除文件
+ (BOOL)removeFile:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isRemove = NO;
    if ([fileManager fileExistsAtPath:path]) {
        NSError *error;
        isRemove = [fileManager removeItemAtPath:path error:&error];
        if (error) {
            ZHLog(@"移除文件%@失败: %@", path, error);
            isRemove = NO;
        }
    }
    return isRemove;
}

/// 获取目录下一级子文件
+ (NSArray<NSString *> *)subFilesWithPath:(NSString *)path {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL isExist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if (isExist) {
        if (isDir) {
            NSError *error = nil;
            NSArray *dirArray = [fileManager contentsOfDirectoryAtPath:path error:&error];
            if (!error) {
                return dirArray;
            } else {
                ZHLog(@"获取目录%@下的子目录失败:%@", path, error);
                return nil;
            }
        }
    }
    
    return nil;
}

/// 递归获取对应位置中的第一个该后缀的文件，如果没有的话，返回nil
/// @param dirpath 文件位置
/// @param suffix 后缀名
- (NSString *)getFaceFileNameWithDirPath:(NSString *)dirpath suffix:(NSString *)suffix{
    NSFileManager *fileManger = [NSFileManager defaultManager];
    NSString *faceFileName;
    BOOL isDir = NO;
    BOOL isExist = [fileManger fileExistsAtPath:dirpath isDirectory:&isDir];
    if (isExist) {
        if (!isDir) {
            NSString *fileName = [[dirpath componentsSeparatedByString:@"/"] lastObject];
            if ([fileName hasSuffix:suffix]) {
                faceFileName = fileName;
                return dirpath;
                ZHLog(@"%@", dirpath);
            }
            
        }else{//是文件夹
            NSArray * dirArray = [fileManger contentsOfDirectoryAtPath:dirpath error:nil];
            NSString * subPath = nil;
            for (NSString * str in dirArray) {
                subPath  = [dirpath stringByAppendingPathComponent:str];
                BOOL issubDir = NO;
                [fileManger fileExistsAtPath:subPath isDirectory:&issubDir];
                faceFileName = [self getFaceFileNameWithDirPath:subPath suffix:suffix];
                if ([str hasSuffix:suffix]) {//如果第一个循环里面已经找到了xml文件，那么后面就没必要再轮训下去了
                    ZHLog(@"已经找到了，不许再轮训了");
                    break;
                }
            }
        }
    }else{
        ZHLog(@"this path is not exist!");
    }
    return faceFileName;
}
@end
